﻿using NLog.Filters;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Text;

namespace VIA.Project.Domain.Services
{
    [AddINotifyPropertyChangedInterface]
    public class ToleranceItem<T> where T : IComparable<T>
    {
        public T Value { get; set; } = default;
        public T MinValue { get; set; } = default;
        public T MaxValue { get; set; } = default;
        public string Name { get; set; }
        public string Unit { get; set; } = "mm";
        public bool IsEnable { get; set; } = true;
        public bool IsUseMinValue { get; set; } = true;
        public bool IsUseMaxValue { get; set; } = true;
        public ToleranceItem() { }
        public ToleranceItem(string name)
        {
            Name = name;
        }
        public ToleranceItem(string name, string unit)
        {
            Name = name;
            Unit = unit;
        }
        public ToleranceItem(string name, string unit, bool isUseMinValue, bool isUseMaxValue)
        {
            Name = name;
            Unit = unit;
            IsUseMinValue = isUseMinValue;
            IsUseMaxValue = isUseMaxValue;
        }

        public bool IsOK()
        {
            return IsOK(Value);
        }

        public bool IsOK(T value)
        {
            if (!IsEnable) return true;
            if (value is bool b) return b;
            if (value == null) return false;
            bool isOK = true;
            if (IsUseMinValue)
            {
                if (MinValue == null)
                {
                    return false;
                }
                isOK &= value.CompareTo(MinValue) >= 0;
            }
            if (IsUseMaxValue)
            {
                if (MaxValue == null)
                {
                    return false;
                }
                isOK &= value.CompareTo(MaxValue) <= 0;
            }
            return isOK;
        }
    }

}
